package com.test.springboot.mybatis.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author chenxubing
 * @version $Id: CustomDateTimeDeserializer.java, v 0.1 2018-09-08 下午7:25 chenxubing Exp $$
 */
public class CustomDateTimeDeserializer  extends JsonDeserializer<Date>{

    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(p.getText());
        } catch (ParseException e) {
            try {
                return new SimpleDateFormat("yyyy-MM-dd").parse(p.getText());
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
        }
        return null;
    }

}